1 module getmodules;
2 import redub.plugin.api;
3 class GetModulePlugin : RedubPlugin
4 {
5     void preGenerate(){}
6     void postGenerate(){}
7 	/**
8 	 * Utility to generate a list of the modules found in all sourcePaths from the current dependency.
9 	 * Use it with:
10 	 ```json
11 	 "preBuildPlugins": {
12 	 	"getmodules": "ct_assets/game_modules.txt"
13 	 }
14 	 ```
15 	 */
16     extern(C) ref RedubPluginStatus preBuild(RedubPluginData input, out RedubPluginData output, const ref string[] args, ref return RedubPluginStatus status)
17     {
18 		import std.file;
19 		import std.path;
20 		import std.array:replace;
21 
22 		if(args.length != 1)
23 			return status = RedubPluginStatus(RedubPluginExitCode.error, "Usage: \"getmodules\": [\"outputFileName\"]");
24 		string outputPath = args[0];
25 		if(exists(outputPath) && isDir(outputPath))
26 			return status = RedubPluginStatus(RedubPluginExitCode.error, "Invalid output path '"~outputPath~"', the output path is a directory");
27 		if(outputPath.length == 0)
28 			return status = RedubPluginStatus(RedubPluginExitCode.error, "Invalid output path '"~outputPath~"', the output path is empty.");
29 
30 		string getModulesFile;
31 
32 		foreach(string inputPath; input.sourcePaths)
33 		{
34 			import std.algorithm.searching;
35 			foreach(DirEntry e; dirEntries(inputPath, "*.d", SpanMode.depth))
36 			{
37 				if(countUntil(e.name, "gamescript") == -1)
38 					continue;
39 				string file = e.name;
40 				if(getModulesFile != "")
41 					getModulesFile~="\n";
42 				//Remove .d, change / or \ to .
43 
44 				file = relativePath(file, inputPath)[0..$-2];
45 				getModulesFile~= file.replace(dirSeparator[0], '.');
46 			}
47 		}
48 		string outDir = dirName(outputPath);
49 		if(!std.file.exists(outDir))
50 			std.file.mkdirRecurse(outDir);
51 
52 		std.file.write(outputPath, getModulesFile);
53 		return status = RedubPluginStatus(RedubPluginExitCode.success, "getModule plugin generated file "~outputPath);
54     }
55     void postBuild(){}
56 }
57 mixin PluginEntrypoint!(GetModulePlugin);